home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / vla / ctut3vla / array.txt < prev    next >
Text File  |  1993-06-11  |  7KB  |  136 lines

  1.                        
  2.                        [%] An Explanation of Arrays [%]
  3.                               By : Desolation/VLA
  4.  
  5.  
  6.    Here is the next tutorial in C which tells you about arrays.  Arrays are
  7. a very important topic in programming and you will use it a lot, so I thought 
  8. I would start it out with an explanation of its usage, how to implement it, 
  9. etc.  The last tutorial on loops has yet to be fully completed, but you
  10. should learn how to do the other looping structures on your own such as the
  11. while loop, the do-while loop, etc.  It's not that hard to figure out, they
  12. are pretty much the same.
  13.  
  14.    Ok, here we go.  An array is just what it sounds like.  It's an assortment
  15. of the same type of objects in an ordered fashion which has just one variable
  16. name associated with it.  It has an index which allows you to access each part 
  17. of the array easily.  In the actual computer, this would be a separate memory 
  18. location for each individual part of the array (just trivia).  A simple way to 
  19. visualize an array would be like this:
  20.  
  21. Let's say you make an array of integers called "integer_array" and want the 
  22. size of the array to be of length 5.  This is what it might look like :
  23.  
  24.                 ┌───────────────────┐
  25. integer_array   │ 2 │321│ 12│576│378│   <- Integer Values 
  26. (array name)    └───────────────────┘                         
  27.                   0   1   2   3   4     <- Index Numbers     
  28.  
  29.  
  30. First off, the numbers in each slot isn't specific, just in case you're 
  31. confused.  They are whatever you want to put in there, just as long as they
  32. are all of the type you specified in the beginning (which was of type integer).
  33. NOTICE!  This is very important to remember.  The index begins with 0, not 
  34. 1.  So when you go to reference the first element in the array, you use
  35. 0 and not 1.  This is important because some other languages use 1 to begin 
  36. with.  This also makes the ending index number 1 off.  In our example I said 
  37. of length 5, which means 5 elements, but the numbers go from 0 to 4.  If you
  38. look, there are 5 elements there, so keep this in mind, because if you 
  39. are going through writing code that uses arrays, and miss this fact, you'll
  40. screw up your code.  
  41.  
  42. The index of the array is used to pull out specific elements in the array.
  43. For example if you look at the index #3 of the integer_array we declared, the
  44. value there would be "576".  Nothing too difficult, but how does this all fit
  45. into C you might ask?  Well, as I run you around some more, I'll final give
  46. you the low down on how this is actually implemented in C.
  47.  
  48. The formal declaration looks like this : type variable_name[size];
  49. Type is any type you want it to be, integer, float, character, etc.
  50. Variable_Name is the name you give it (for example integer_array like before).
  51. And the size is the number of elements you want in the array (5 in our
  52.    example from before.)
  53.  
  54. For our example, here is what it would like look if we declared it in C :
  55.  
  56.    int integer_array[5];
  57.  
  58. Got that?  Pretty simple so far eh?  Now, let's say we haven't assigned 
  59. anything to our array yet as we did in our previous example.  How do we
  60. get those values into the array?  Well, it should do something with the
  61. index value right?  How else would we access the elements without the index?
  62. After we declare the array, we can assign the values in this way :
  63.  
  64.    integer_array[0] = 2;
  65.    integer_array[1] = 321;
  66.    integer_array[2] = 12;
  67.    integer_array[3] = 576;
  68.    integer_array[4] = 378;
  69.  
  70. The value in the brackets now acts as the index value.  The first one says
  71. to assign the integer value "2" to the first element in the array (element 0).
  72. The second assignment statement says to assign "321" to the 2nd element in
  73. the array (element 1) and so on.  No problems.
  74.  
  75.    Now for a little application.  To assign to the array, you will hardly
  76. every assign every index one by one like I did up above.  Instead, you use
  77. a variable for the indexing value and store that way.  Let me show you some
  78. code first :
  79.  
  80.    int i;
  81.    int integer_array[5];
  82.  
  83.    for(i=0; i<5; i++) integer_array[i] = 400;
  84.  
  85. Now looking at this code, what does it do?  First off we declared our array
  86. and another integer which I called 'i'.  This will be used as the indexing
  87. value.  We start up by using a for loop structure.  This will allow us to 
  88. increment 'i' by 1 every time we go through starting from 0 and going to 5.
  89. So we start at i=0.  Then we execute the next part which says 
  90.    "integer_array[i] = 400;"
  91.  
  92. But 'i' is currently 0 right now correct?  So, integer_array[0] gets the
  93. value of 400.  Then we go back to the for looping structure because we 
  94. aren't done yet.  We increment 'i' by 1 to 1 (because we started at 0) and 
  95. execute the rest again.  Now since 'i' is 1, we say integer_array[1] = 400
  96. which would stick the value of 400 in the #1 slot of the array and so on.
  97. This is what it would look like when we finish :
  98.  
  99.                 ┌───────────────────┐
  100. integer_array   │400│400│400│400│400│   <- Integer Values 
  101. (array name)    └───────────────────┘                         
  102.                   0   1   2   3   4     <- Index Numbers     
  103.  
  104.  
  105. We stared at index 0, put in the value of 400, then incremented the index
  106. value by 1, stored in the value of 400, and kept going until we got to the 
  107. end of the array.  A for loop is commonly used for arrays because you can 
  108. access the index values like a variable and store easily into each element
  109. of the array.
  110.  
  111.    Now it's time for you to play with this and see what you can do with it.
  112. After all, you are the one who needs to figure out how to use this in a more
  113. complex way to suit your own needs.  A good place to start is to use a 
  114. variable for an assignment, such as :
  115.  
  116.    integer_array[i] = some_variable_with_some_operation;
  117.  
  118. and so on.  Try creating a small program that will prompt for 5 integer
  119. values, then store each value into the array using a for loop.  Then pull
  120. out and examine the values in the array and print them back to the screen.
  121. Once again, if you need ANY help or have ANY questions, ask on Phantasm.
  122. I would be more than willing to help you out.  Arrays are very important, and
  123. I wouldn't be surprised if you use them in a great deal of your coding once 
  124. you start writing anything moderatly complex, so ask away and understand 
  125. what this is all about!  In any event, may your code be bug free and 
  126. efficient!
  127.  
  128.  
  129. Desolation/VLA
  130. decko@u.washington.edu
  131. Phantasm (206)232-5912
  132. Now supporting USR Dual Standard
  133.  
  134. Coming Up Next Time : Multi-dimensional arrays?  Pointers and array pointers?
  135.                       Who knows? I'm not organized enough to know.
  136.